/*
 * Copyright (C) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.megagoodsoftware.MediaShare;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.ActivityNotFoundException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.ViewAnimator;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.BitmapDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.net.Uri;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import com.github.droidfu.adapters.WebGalleryAdapter;
import com.megagoodsoftware.smugmug.Photo;
import com.megagoodsoftware.smugmug.PhotoList;
import com.megagoodsoftware.smugmug.PhotoSize;

/**
 * Activity that displays a photo along with its title and the date at which it was taken.
 * This activity also lets the user set the photo as the wallpaper.
 */
public class ViewPhotoActivity extends Activity implements View.OnClickListener {

    static final String ACTION = "com.megagoodsoftware.MediaShare.FLICKR_PHOTO";

    private static final String RADAR_ACTION = "com.google.android.radar.SHOW_RADAR";
    private static final String RADAR_EXTRA_LATITUDE = "latitude";
    private static final String RADAR_EXTRA_LONGITUDE = "longitude";

    private static final String EXTRA_PHOTO = "com.megagoodsoftware.MediaShare.photo";

    private static final String WALLPAPER_FILE_NAME = "wallpaper";

    private static final int REQUEST_CROP_IMAGE = 42;

    private Photo mPhoto;

    private ViewAnimator mSwitcher;
    private ImageView mPhotoView;
    private ViewGroup mContainer;

    private AsyncTask<?, ?, ?> mTask;
    private TextView mPhotoTitle;
    private TextView mPhotoDate;
	private View mMenuSeparator;
	private View mMenuSet;
	private int mImageNumber;

	private FlingGallery mGallery;

	@Override
    public boolean onTouchEvent(MotionEvent event)
	{
        return mGallery.onGalleryTouchEvent(event);
    }

	public void setFullscreen() {
	        requestWindowFeature(Window.FEATURE_NO_TITLE);
	        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
	                WindowManager.LayoutParams.FLAG_FULLSCREEN);
	    }
	    public void setNoTitle() {
	        requestWindowFeature(Window.FEATURE_NO_TITLE);
	    }
	 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mPhoto = getPhoto();
        setFullscreen();
        setNoTitle();
        WebGalleryAdapter webAdapter = new WebGalleryAdapter(this);
        webAdapter.setImageUrls(loadImages());
   	        mGallery = new FlingGallery(this);
    	        mGallery.setPaddingWidth(5);
    	        mGallery.setAdapter(webAdapter,mImageNumber);
    		
    		LinearLayout layout = new LinearLayout(getApplicationContext());
            layout.setOrientation(LinearLayout.VERTICAL);

    		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
    				LinearLayout.LayoutParams.FILL_PARENT,
    				LinearLayout.LayoutParams.FILL_PARENT);

//    		layoutParams.setMargins(10, 10, 10, 10);
    		layoutParams.weight = 1.0f;
      
            layout.addView(mGallery, layoutParams);
            setContentView(layout);

    }

    private List<String> loadImages() {
    	PhotoList imageList = ((MediaShareApp) getApplicationContext()).getPhotoList();
    	List<String> imageUrls = new ArrayList<String>();
    	for (int i = 0; i < imageList.getCount(); i++)
    	{
    		if (imageList.get(i).getId().equals(mPhoto.getId()))
    			mImageNumber = i;
    		imageUrls.add(imageList.get(i).getMediumUrl());
    		
    	}
		return imageUrls;
	}
	/**
     * Starts the ViewPhotoActivity for the specified photo.
     *
     * @param context The application's environment.
     * @param photo The photo to display and optionally set as a wallpaper.
     */
    static void show(Context context, Photo photo) {
        final Intent intent = new Intent(ACTION);
        intent.putExtra(EXTRA_PHOTO, photo);
        context.startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mTask != null && mTask.getStatus() != AsyncTask.Status.RUNNING) {
            mTask.cancel(true);
        }
    }

    /**
     * Loads the {@link com.megagoodsoftware.smugmug.Photo} to display
     * from the intent used to start the activity.
     *
     * @return The photo to display, or null if the photo cannot be found.
     */
    public Photo getPhoto() {
        final Intent intent = getIntent();
        final Bundle extras = intent.getExtras();

        Photo photo = null;
        if (extras != null) {
            photo = extras.getParcelable(EXTRA_PHOTO);
        }

        return photo;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.view_photo, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_item_radar:
                onShowRadar();
                break;
        }
        return super.onMenuItemSelected(featureId, item);
    }

    private void onShowRadar() {
        new ShowRadarTask().execute(mPhoto);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.menu_back:
                onBack();
                break;
//            case R.id.menu_set:
//                onSet();
//                break;
        }
    }

    private void onSet() {
        mTask = new CropWallpaperTask().execute(mPhoto);
    }

    private void onBack() {
        finish();
    }

    /**
     * If we successfully loaded a photo, send it to our future self to allow
     * for fast display rotation. By doing so, we avoid reloading the photo
     * from the network when the activity is taken down and recreated upon
     * display rotation.
     *
     * @return The Bitmap displayed in the ImageView, or null if the photo
     *         wasn't loaded.
     */
    @Override
    public Object onRetainNonConfigurationInstance() {
        final Drawable d = mPhotoView.getDrawable();
        return d != null ? ((BitmapDrawable) d).getBitmap() : null;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Spawns a new task to set the wallpaper in a background thread when/if
        // we receive a successful result from the image cropper.
        if (requestCode == REQUEST_CROP_IMAGE) {
            if (resultCode == RESULT_OK) {
                mTask = new SetWallpaperTask().execute();
            } else {
                cleanupWallpaper();
                showWallpaperError();
            }
        }
    }

    private void showWallpaperError() {
        Toast.makeText(ViewPhotoActivity.this, R.string.error_cannot_save_file,
                Toast.LENGTH_SHORT).show();
    }

    private void showWallpaperSuccess() {
        Toast.makeText(ViewPhotoActivity.this, R.string.success_wallpaper_set,
                Toast.LENGTH_SHORT).show();
    }

    private void cleanupWallpaper() {
        deleteFile(WALLPAPER_FILE_NAME);
        mSwitcher.showNext();
    }

 
    /**
     * Background task to crop a large version of the image. The cropped result will
     * be set as a wallpaper. The tasks sarts by showing the progress bar, then
     * downloads the large version of hthe photo into a temporary file and ends by
     * sending an intent to the Camera application to crop the image.
     */
    private class CropWallpaperTask extends AsyncTask<Photo, Void, Boolean> {
        private File mFile;

        @Override
        public void onPreExecute() {
            mFile = getFileStreamPath(WALLPAPER_FILE_NAME);
            mSwitcher.showNext();
        }

        public Boolean doInBackground(Photo... params) {
            boolean success = false;

            OutputStream out = null;
            try {
                out = openFileOutput(mFile.getName(), MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE);
                Flickr.get().downloadPhoto(params[0], PhotoSize.LARGE, out);
                success = true;
            } catch (FileNotFoundException e) {
                android.util.Log.e(Flickr.LOG_TAG, "Could not download photo", e);
                success = false;
            } catch (IOException e) {
                android.util.Log.e(Flickr.LOG_TAG, "Could not download photo", e);
                success = false;
            } finally {
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        success = false;
                    }
                }
            }

            return success;
        }

        @Override
        public void onPostExecute(Boolean result) {
            if (!result) {
                cleanupWallpaper();
                showWallpaperError();
            } else {
                final int width = getWallpaperDesiredMinimumWidth();
                final int height = getWallpaperDesiredMinimumHeight();

                final Intent intent = new Intent("com.android.camera.action.CROP");
                intent.setClassName("com.android.camera", "com.android.camera.CropImage");
                intent.setData(Uri.fromFile(mFile));
                intent.putExtra("outputX", width);
                intent.putExtra("outputY", height);
                intent.putExtra("aspectX", width);
                intent.putExtra("aspectY", height);
                intent.putExtra("scale", true);
                intent.putExtra("noFaceDetection", true);
                intent.putExtra("output", Uri.parse("file:/" + mFile.getAbsolutePath()));

                startActivityForResult(intent, REQUEST_CROP_IMAGE);
            }

            mTask = null;
        }
    }

    /**
     * Background task to set the cropped image as the wallpaper. The task simply
     * open the temporary file and sets it as the new wallpaper. The task ends by
     * deleting the temporary file and display a message to the user.
     */
    private class SetWallpaperTask extends AsyncTask<Void, Void, Boolean> {
        public Boolean doInBackground(Void... params) {
            boolean success = false;
            InputStream in = null;
            try {
                in = openFileInput(WALLPAPER_FILE_NAME);
                setWallpaper(in);
                success = true;
            } catch (IOException e) {
                success = false;
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        success = false;
                    }
                }
            }
            return success;
        }

        @Override
        public void onPostExecute(Boolean result) {
            cleanupWallpaper();

            if (!result) {
                showWallpaperError();
            } else {
                showWallpaperSuccess();
            }

            mTask = null;
        }
    }

    private class ShowRadarTask extends AsyncTask<Photo, Void, Flickr.Location> {
        public Flickr.Location doInBackground(Photo... params) {
            return Flickr.get().getLocation(params[0]);
        }

        @Override
        public void onPostExecute(Flickr.Location location) {
            if (location != null) {
                final Intent intent = new Intent(RADAR_ACTION);
                intent.putExtra(RADAR_EXTRA_LATITUDE, location.getLatitude());
                intent.putExtra(RADAR_EXTRA_LONGITUDE, location.getLongitude());

                try {
                    startActivity(intent);
                } catch (ActivityNotFoundException e) {
                    Toast.makeText(ViewPhotoActivity.this, R.string.error_cannot_find_radar,
                        Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(ViewPhotoActivity.this, R.string.error_cannot_find_location,
                        Toast.LENGTH_SHORT).show();
            }
        }
    }
}
